package com.repository.impl;

import com.entity.*;
import com.repository.DoctorRepository;
import com.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Vector;

public class DoctorRepositoryImpl implements DoctorRepository {
    @Override
    public int isExistDoctorByWorkId(String workId) {
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        DoctorRepository doctorRepository=sqlSession.getMapper(DoctorRepository.class);
        int rs=doctorRepository.isExistDoctorByWorkId(workId);
        sqlSession.close();
        return rs;
    }

    @Override
    public void changeRegisterState(int registerDocId) {
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        DoctorRepository doctorRepository=sqlSession.getMapper(DoctorRepository.class);
        doctorRepository.changeRegisterState(registerDocId);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public Doctor findDoctorByName(String addDoctorName) {
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        DoctorRepository doctorRepository=sqlSession.getMapper(DoctorRepository.class);
        Doctor doctor=doctorRepository.findDoctorByName(addDoctorName);
        sqlSession.close();
        return doctor;
    }

    @Override
    public int addDoctorLogin(Doctor doctor) {
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        DoctorRepository doctorRepository=sqlSession.getMapper(DoctorRepository.class);
        int rs=doctorRepository.addDoctorLogin(doctor);
        sqlSession.commit();
        sqlSession.close();
        return rs;
    }

    @Override
    public int isDoctorLogin(Doctor doctor) {
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        DoctorRepository doctorRepository=sqlSession.getMapper(DoctorRepository.class);
        int rs=doctorRepository.isDoctorLogin(doctor);
        sqlSession.close();
        return rs;
    }

    @Override
    public Doctor findDoctorByWorkId(String workId) {
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        DoctorRepository doctorRepository=sqlSession.getMapper(DoctorRepository.class);
        Doctor doctor=doctorRepository.findDoctorByWorkId(workId);
        sqlSession.close();
        return doctor;
    }

    @Override
    public Vector<RegisterDoc> findAllRegisterDocByWorkId(String workId,int state) {
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        DoctorRepository doctorRepository=sqlSession.getMapper(DoctorRepository.class);
        Vector<RegisterDoc> registerDocs=doctorRepository.findAllRegisterDocByWorkId(workId,state);
        sqlSession.close();
        return registerDocs;
    }

    @Override
    public Vector<Drug> findAllDrugsByWorkId(String workId) {
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        DoctorRepository doctorRepository=sqlSession.getMapper(DoctorRepository.class);
        Vector<Drug> drugs=doctorRepository.findAllDrugsByWorkId(workId);
        sqlSession.close();
        return drugs;
    }

    @Override
    public Vector<Diagnose> findAllDiagnosesByWorkId(String workId) {
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        DoctorRepository doctorRepository=sqlSession.getMapper(DoctorRepository.class);
        Vector<Diagnose> diagnoses=doctorRepository.findAllDiagnosesByWorkId(workId);
        sqlSession.close();
        return diagnoses;
    }

    @Override
    public int addDiagnose(Diagnose diagnose) {
        SqlSession sqlSession= MybatisUtil.getSqlSession();
        DoctorRepository doctorRepository=sqlSession.getMapper(DoctorRepository.class);
        int rs=doctorRepository.addDiagnose(diagnose);
        sqlSession.commit();
        sqlSession.close();
        return rs;
    }

    @Override
    public int addDrug(Drug drug) {
        SqlSession sqlSession=MybatisUtil.getSqlSession();
        DoctorRepository doctorRepository=sqlSession.getMapper(DoctorRepository.class);
        int rs=doctorRepository.addDrug(drug);
        sqlSession.commit();
        sqlSession.close();
        return rs;
    }
}
